import Caption from '~/components/text/caption'
import { InlineCode } from '~/components/text/code'

export const meta = {
  editUrl:
    'pages/docs/integrations/integrations-docs-mdx/project-level-apis/utils-inside-integration-utils.mdx',
  lastEdited: '2019-10-15T22:03:42.000Z'
}

## Utilities inside @zeit/integration-utils

If you are using `@zeit/integration-utils` to create your [UIHook](#creating-an-integration/step-2-creating-a-uihook), the following example shows how to use utilities within your Integration:

```js
const { withUiHook } = require('@zeit/integration-utils')

module.exports = withUiHook(async ({ payload, zeitClient }) => {
  if (payload.action === 'add-env') {
    const mongo_url = 'this is your mongo url'
    const secretName = await zeitClient.ensureSecret('mongo_url', mongo_url)
    await zeitClient.upsertEnv(payload.projectId, 'MONGO_URL', secretName)

    return `
      <Page>
        Environment variable added.
        <Button small action="view">Go Back</Button>
      </Page>
    `
  }

  return `
    <Page>
      <Button action="add-env">Add Env</Button>
    </Page>
  `
})
```

<Caption>
  An example UIHook that uses <InlineCode>@zeit/integration-utils</InlineCode>{' '}
  utilities.
</Caption>

Based on the example above:

- `zeitClient.ensureSecret` will create a unique ZEIT secret based on the hash value of `mongo_url`, then returns the name of secret
- If the secret already exists, it will not be duplicated for the given value
- The value of `secretName` will be, for example; `mongo_url_s2kj23jh55`
- The secret is added to the project (using `payload.projectId`) through the `zeitClient.upsertEnv` method

Some considerations when using `zeitClient.upsertEnv`:

- In order to add a project level environment variable, there should be a `projectId`
- You can get the projectId from the `payload` object if the user is accessing the Integration installation from within a project
- Otherwise, if the user is inside a team view, you can hide the `Add Env` button or ask the user to switch to accessing the Integration from within a project
